#coding=utf-8
import datetime
import time
import collections
import logging
from main import models
from django.db.models import Q
#from itertools import chain

logger = logging.getLogger('main')

def menu_struct(menulist):
    subordinate_dict = collections.defaultdict(list)
    for s in menulist:
        subordinate_dict[s.parent_id].append(s)

    menu_toplist = subordinate_dict.get(0, [])
    
    return_data = []
    
    for menu in menu_toplist:
        menu_top = {}
        menu_top["id"] = menu.id
        menu_top["name"] = menu.name
        menu_top["icon"] = menu.icon
        
        menu_seclist = subordinate_dict.get(menu.id, [])
        
        menu_data = []
        for menu_sec in menu_seclist:
            adv_mess = {}
            adv_mess["id"] = menu_sec.id
            adv_mess["name"] = menu_sec.name
            adv_mess["url"] = menu_sec.url
            menu_data.append(adv_mess)
        menu_top["menu_sec"] = menu_data
        return_data.append(menu_top)
    return return_data
    